// stores/contacts.js
import {
	defineStore
} from 'pinia';
import {
	getDynamicApi,
	puthandUpApi,
	addPutDynamicApi,
	delDynamicApi,
	addCommentApi,
} from '@/api/dynamic';

export const useDynamicStore = defineStore('dynamic', {
	state: () => {
		return {
			userId: '', // 在首页登录成功加载时 获取值
			spaceDynamicMap: {}, // 用户对应的空间动态
			userSpaceDynamic: [], // 当前用户相关的空间动态
			squareDynamicMap: {}, // 用户对应的广场动态
			userSquareDynamic: [], // 当前广场动态
			spaceImg: {}, // 查询用户详情时的空间动态图片
			squareImg: {}, // 查询用户详情时的广场动态图片
		};
	},
	getters: {
		userSpaceDynamicMap: (state) => state.userSpaceDynamic.reduce((pre, v) => {
			pre[v.id] = v;
			return pre;
		}, {}),
		userSquareDynamicMap: (state) => state.userSquareDynamic.reduce((pre, v) => {
			pre[v.id] = v;
			return pre;
		}, {}),
	},
	actions: {
		// 获取用当前用户相关的空间动态
		async getUserSpaceDynamic() {
			try {
				let {
					data
				} = await getDynamicApi({
					page: 1,
					limit: 10,
					type: 1,
					uid: this.userId,
				});
				if (data.data.code == 1) {
					this.userSpaceDynamic = data.data.data.records.map(item => {
						if (item.fileList) {
							item.fileList = JSON.parse(item.fileList);
						}
						return item;
					});
					return data.data.data;
				} else {
					throw Error(data);
				}
			} catch (e) {
				throw Error(e)
			}
		},
		// 上拉获取用当前用户相关的空间动态
		async addUserSpaceDynamic({
			page = 1,
			limit = 10,
		}) {
			try {
				let {
					data
				} = await getDynamicApi({
					page,
					limit,
					type: 1,
					uid: this.userId,
				});
				if (data.data.code == 1) {
					this.userSpaceDynamic.splice((page - 1) * limit, page * limit, ...data.data.data.records
						.map(item => {
							if (item.fileList) item.fileList = JSON.parse(item.fileList);
							return item;
						}));
					return data.data.data;
				} else {
					throw Error(data);
				}
			} catch (e) {
				throw Error(e)
			}
		},
		// 获取用用户相关的空间动态
		async getSpaceDynamic(id) {
			try {
				let {
					data
				} = await getDynamicApi({
					page: 1,
					limit: 10,
					publisher: id,
					type: 1,
					uid: id,
				});
				if (data.data.code == 1) {
					this.spaceDynamicMap[id] = data.data.data.records;
				} else {
					throw Error(data);
				}
			} catch (e) {
				throw Error(e)
			}
		},
		// 查询用户详情时的空间动态图片
		async getSpaceImg(id) {
			try {
				let {
					data
				} = await getDynamicApi({
					page: 1,
					limit: 4,
					publisher: id,
					type: 1,
					uid: id,
					isFile: 1,
					appUserIndex: 1,
				});
				if (data.data.code == 1) {
					this.spaceImg[id] = data.data.data.records.map(item => JSON.parse(item.fileList)).flat()
						.slice(0, 4);
				} else {
					throw Error(data);
				}
			} catch (e) {
				throw Error(e)
			}
		},

		// 获取用当前广场动态
		async getUserSquareDynamic() {
			try {
				let {
					data
				} = await getDynamicApi({
					page: 1,
					limit: 10,
					type: 2,
					uid: this.userId,
				});
				if (data.data.code == 1) {
					this.userSquareDynamic = data.data.data.records.map(item => {
						if (item.fileList) {
							item.fileList = JSON.parse(item.fileList);
						}
						return item;
					});
					return data.data.data;
				} else {
					throw Error(data);
				}
			} catch (e) {
				throw Error(e)
			}
		},
		// 上拉获取用当前用户相关的广场动态
		async addUserSquareDynamic({
			page = 1,
			limit = 10,
		}) {
			try {
				let {
					data
				} = await getDynamicApi({
					page,
					limit,
					type: 2,
					uid: this.userId,
				});
				if (data.data.code == 1) {
					this.userSquareDynamic.splice((page - 1) * limit, page * limit, ...data.data.data
						.records.map(item => {
							if (item.fileList) item.fileList = JSON.parse(item.fileList);
							return item;
						}));
					return data.data.data;
				} else {
					throw Error(data);
				}
			} catch (e) {
				throw Error(e)
			}
		},
		// 获取用当前用户相关的广场动态
		async getSquareDynamic(id) {
			try {
				let {
					data
				} = await getDynamicApi({
					page: 1,
					limit: 10,
					publisher: id,
					type: 2,
					uid: id,
				});
				if (data.data.code == 1) {
					this.squareDynamicMap[id] = data.data.data.records;
				} else {
					throw Error(data);
				}
			} catch (e) {
				throw Error(e)
			}
		},
		// 查询用户详情时的广场动态图片
		async getSquareImg(id) {
			try {
				let {
					data
				} = await getDynamicApi({
					page: 1,
					limit: 4,
					publisher: id,
					type: 2,
					uid: id,
					isFile: 1,
					appUserIndex: 1,
				});
				if (data.data.code == 1) {
					this.squareImg[id] = data.data.data.records.map(item => JSON.parse(item.fileList))
						.flat().slice(0, 4);
				} else {
					throw Error(data);
				}
			} catch (e) {
				throw Error(e)
			}
		},

		// 点赞/取消点赞
		async putHandUp({
			type,
			fid,
			operationType,
		}) {
			let {
				data
			} = await puthandUpApi({
				type,
				fid,
				uid: this.userId,
				operationType,
			});

			if (data.data.code === 1) {
				let index = -1;
				switch (type) {
					case 3:
						index = this.userSpaceDynamic.findIndex(item => item.id === fid);
						this.userSpaceDynamic[index].loginUserEvaluateLikeCount = operationType === 1 ? 1 :
							0;
						this.userSpaceDynamic[index].greatCount += operationType === 1 ? 1 : -1;
						break;
					case 4:
						index = this.userSquareDynamic.findIndex(item => item.id === fid);
						this.userSquareDynamic[index].loginUserEvaluateLikeCount = operationType === 1 ? 1 :
							0;
						this.userSquareDynamic[index].greatCount += operationType === 1 ? 1 : -1;
						break;
					default:
						break;
				}
			} else {
				throw Error(data);
			}
		},
		// 新增动态
		async addDynamic({
			content,
			fileList,
			type,
		}) {
			let {
				data
			} = await addPutDynamicApi({
				publisher: this.userId,
				content,
				fileList,
				type,
			});

			if (data.data.code == 1) {
				switch (type) {
					case 1:
						this.userSpaceDynamic.splice(0, 0, {
							...data.data.data.data,
							fileList: JSON.parse(fileList)
						});
						break;
					case 2:
						this.userSquareDynamic.splice(0, 0, {
							...data.data.data.data,
							fileList: JSON.parse(fileList)
						});
						break;
					default:
						break;
				}
				return true;
			} else {
				throw Error(data);
			}
		},
		// 删除动态
		async delDynamic(id) {
			let {
				data
			} = await delDynamicApi({
				ids: JSON.stringify([id])
			});

			console.log(data);
			if (data.data.code == 1) {
				let index = this.userSpaceDynamic.findIndex(item => item.id == id);
				index !== -1 && this.userSpaceDynamic.splice(index, 1)
			} else {
				throw Error(data);
			}
		},
		// 新增评论
		async addComment({
			articleId,
			type,
			grade,
			receiver,
			comments,
		}) {
			let {
				data
			} = await addCommentApi({
				articleId,
				type,
				grade,
				receiver,
				comments,
				commentator: this.userId,
			});

			let index = -1;
			if (data.data.code == 1) {
				switch (type) {
					case 2:
						index = this.userSpaceDynamic.findIndex(item => item.id == articleId);
						this.userSpaceDynamic[index].leaveacommentCount += 1;
						break;
					case 3:
						index = this.userSquareDynamic.findIndex(item => item.id == articleId);
						this.userSquareDynamic[index].leaveacommentCount += 1;
						break;
					default:
						break;
				}
				return data.data.data.data[0];
			} else {
				throw Error(data);
			}
		},
	},
	unistorage: {
		paths: ['userId'] // 需要缓存的路径
	}
});
